package sample.common;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.omg.CORBA.INTERNAL;
import sample.model.StandardData;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class DrawStandardLineChartImpl extends Application implements IDrawLineChart{

    private List<StandardData> list;
    private Map<Integer,List<StandardData>> collect = new LinkedHashMap<>();
    public DrawStandardLineChartImpl(List<StandardData> list){
        this.list = list;
        setCollect();
    }
    public void setCollect(){
        collect = list.stream().collect(Collectors.groupingBy(StandardData::getMonth));
//        list.stream().collect(Collectors.groupingBy(StandardData::getMonth))
    }

    @Override
        public void start(Stage primaryStage) throws Exception {
            primaryStage.setTitle("LineChart");

            NumberAxis xAxis = new NumberAxis(1,1000,10);
            xAxis.setLabel("No of employees");

            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("Revenue per employee");

            LineChart lineChart = new LineChart(xAxis, yAxis);

            XYChart.Series dataSeries1 = new XYChart.Series();
            dataSeries1.setName("2014");
//        for (int i = 0; i < list.size(); i++) {
//            StandardData standardData = list.get(i);
//            dataSeries1.getData().add(new XYChart.Data(i,standardData.getDBT()));
//
//        }
            dataSeries1.getData().add(new XYChart.Data( 1, 567));
            dataSeries1.getData().add(new XYChart.Data( 4, -567));
            dataSeries1.getData().add(new XYChart.Data( 5, 612));
            dataSeries1.getData().add(new XYChart.Data(10, 800));
            dataSeries1.getData().add(new XYChart.Data(20, 780));
            dataSeries1.getData().add(new XYChart.Data(40, 810));
            dataSeries1.getData().add(new XYChart.Data(80, 850));

            lineChart.getData().add(dataSeries1);

            VBox vbox = new VBox(lineChart);

            Scene scene = new Scene(vbox, 400, 200);

            primaryStage.setScene(scene);
            primaryStage.setHeight(300);
            primaryStage.setWidth(1200);
            primaryStage.show();
        }

        public void show2(VBox vbox){
            Stage primaryStage = new Stage();
            primaryStage.setTitle("LineChart");
//            ScrollPane scrollPane = new ScrollPane();
//            scrollPane.setPrefSize(1200,300);

//            vbox.getChildren().add(lineChart);
            ScrollPane scrollPane = new ScrollPane();
            scrollPane.setContent(vbox);
            Scene scene = new Scene(scrollPane, 1200, 300);

            primaryStage.setScene(scene);
            primaryStage.setHeight(500);
            primaryStage.setWidth(1200);

            primaryStage.show();
        }
    public void useToolTip(XYChart.Series series){
        series.getData().forEach((Consumer<XYChart.Data<Integer, Double>>) e -> {
            Tooltip tooltip = new Tooltip();
            tooltip.setText("("+e.getXValue()+","+e.getYValue());
            Tooltip.install(e.getNode(),tooltip);
        });
    }

        public static void main(String[] args) {
            Application.launch(args);
        }

    @Override
    public void drawDBTLineChart() {
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
        NumberAxis xAxis = new NumberAxis();
        XYChart.Series series = new XYChart.Series();
        xAxis.setLabel("第"+(integer)+"月");
        NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("DBT");
        ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
        for (int i = 0; i < list.size(); i++) {
            StandardData standardData = list.get(i);
            XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getDBT());
            series.getData().add(e);

        }
        objects.addAll(series);
        LineChart lineChart = new LineChart(xAxis, yAxis,objects);
        useToolTip(series);
        vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }
    @Override
    public void drawAPLineChart() {
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getAP());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawCCLineChart() {
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Integer> e = new XYChart.Data<>(i,standardData.getCC());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawRHLineChart() {
//        int mon = 1;
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getRH());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawDPTLineChart() {
//        int mon = 1;
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getDPT());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawWDLineChart() {
//        int mon = 1;
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getWD());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawWSLineChart() {
//        int mon = 1;
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getWS());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawTHLineChart() {
//        int mon = 1;
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getTH());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawNRLineChart() {
//        int mon = 1;
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getNR());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawDFLineChart() {
//        int mon = 1;
        VBox vbox = new VBox();
        vbox.setPrefWidth(2000);
        collect.forEach((integer, list) -> {
            NumberAxis xAxis = new NumberAxis();
            XYChart.Series series = new XYChart.Series();
            xAxis.setLabel("第"+(integer)+"月");
            NumberAxis yAxis = new NumberAxis();
            yAxis.setLabel("DBT");
            ObservableList<XYChart.Series> objects = FXCollections.observableArrayList();
            for (int i = 0; i < list.size(); i++) {
                StandardData standardData = list.get(i);
                XYChart.Data<Integer,Double> e = new XYChart.Data<>(i,standardData.getDF());
                series.getData().add(e);

            }
            objects.addAll(series);
            LineChart lineChart = new LineChart(xAxis, yAxis,objects);
            useToolTip(series);
            vbox.getChildren().add(lineChart);
        });
        show2(vbox);
    }

    @Override
    public void drawHSLLineChart() {
    }

    @Override
    public void drawWBTLineChart() {

    }

    @Override
    public void drawVPLineChart() {

    }

    @Override
    public void drawEPLineChart() {

    }

    @Override
    public void drawEHLineChart() {

    }

    @Override
    public void drawSHLineChart() {

    }

    @Override
    public void drawWHLineChart() {

    }

    @Override
    public void drawNHLineChart() {

    }

    @Override
    public void drawVTLineChart() {

    }

    @Override
    public void drawETLineChart() {

    }
}
